
use "data files/sen_r3_local.dta", clear

drop HHOLDSE1 - strtime
gen year_survey = 2005
gen round = 3

***********************
***   Demographic   ***
***********************

decode urbrur, gen(urb)
drop urbrur
ren urb urbrur

gen age = Q1
label var age "Age of respondent"
replace age=. if age==999 
drop Q1

decode Q101, gen(gender)
drop Q101

gen no_school = 0 if Q90!=-1 & Q90!=. & Q90!=99
replace no_school = 1 if Q90==0 
label var no_school "No formal schooling"
gen informal_school = 0 if Q90!=-1 & Q90!=. & Q90!=99
replace informal_school = 1 if Q90==1
label var informal_school "Informal schooling only"

gen primary_school_inc = 0 if Q90!=. & Q90!=99
replace primary_school_inc = 1 if Q90>=2 & Q90<=9
gen primary_school = 0 if Q90!=. & Q90!=99
replace primary_school = 1 if Q90>=3 & Q90<=9
gen secondary_school_inc = 0 if Q90!=. & Q90!=99
replace secondary_school_inc = 1 if Q90>=4 & Q90<=9
gen secondary_school = 0 if Q90!=. & Q90!=99
replace secondary_school = 1 if Q90>=5 & Q90<=9
gen college=  0 if Q90!=. & Q90!=99
replace college = 1 if Q90>=7 & Q90<=9
label var college "Some college, even if incomplete"
drop Q90

gen religion=""
replace religion="None" if Q91==0 | Q91==8 | Q91==9
replace religion="Muslim" if Q91==11 | Q91==12 | Q91==360 | Q91==361 | Q91==362 | Q91==363
replace religion="Christian" if Q91==2 | Q91==3 | Q91==4 | Q91==5 | Q91==10 | Q91==14
* Coded African Independent Church as Christian
replace religion="Traditional" if Q91==6
replace religion="Other" if Q91==13  | Q91==999 | Q91==995
replace religion="Unkown" if Q91==998  | Q91==-1
tab Q91 if religion==""

gen ethnicity = Q79
replace ethnicity = . if Q79 >= 990 | Q79 < 0

gen wolof = 0 if ethnicity != .
replace wolof = 1 if ethnicity == 360

gen toucpeul = 0 if ethnicity != .
replace toucpeul = 1 if ethnicity == 361

gen serer = 0 if ethnicity != .
replace serer = 1 if ethnicity == 362

gen bambara = 0 if ethnicity != .
replace bambara = 1 if  ethnicity == 363 | ethnicity == 367

gen soninke = 0 if ethnicity != .
replace soninke = 1 if ethnicity == 364

gen diola = 0 if ethnicity != .
replace diola = 1 if ethnicity == 365

gen others = 0 if ethnicity != .
replace others = 1 if ethnicity == 368 |  ethnicity == 366 | ethnicity == 369 | ethnicity <= 3

gen language = Q3

gen french_lang = 0 if language != .
replace french_lang = 1 if language == 2

gen wolof_lang = 0 if language != .
replace wolof_lang = 1 if language == 360

gen toucpeul_lang = 0 if language != .
replace toucpeul_lang = 1 if language == 361

gen serer_lang = 0 if language != .
replace serer_lang = 1 if language == 362

gen mand_lang = 0 if language != .
replace mand_lang = 1 if language == 363 

gen diola_lang = 0 if language != .
replace diola_lang = 1 if language == 365

gen other_lang = 0 if lang != .
replace other_lang = 1 if wolof_lang == 0 & toucpeul_lang == 0 & serer_lang == 0 & mand_lang == 0 & diola_lang == 0


********************
***   Economic   ***
********************

gen no_food = 0 if Q8A!=-1 & Q8A!=. & Q8A!=9
replace no_food = 1 if Q8A==2 | Q8A==3 | Q8A==4

gen no_water = 0 if Q8B!=-1 & Q8B!=. & Q8B!=9
replace no_water = 1 if Q8B==2 | Q8B==3 | Q8B==4

gen no_healthcare = 0 if Q8C!=-1 & Q8C!=. & Q8C!=9
replace no_healthcare = 1 if Q8C==2 | Q8C==3 | Q8C==4

gen no_fuel = 0 if Q8D!=-1 & Q8D!=. & Q8D!=9
replace no_fuel = 1 if Q8D==2 | Q8D==3 | Q8D==4

gen no_income = 0 if Q8E!=-1 & Q8E!=. & Q8E!=9
replace no_income = 1 if Q8E==2 | Q8E==3 | Q8E==4
drop Q8A - Q8F


**** Employment variables ***
gen employment_status = "" if Q94!=. & Q94!=-1 & Q94!=9
replace employment_status = "inactive" if  Q94==0
replace employment_status = "unemployed" if Q94==1
replace employment_status = "part-time" if Q94==2 | Q94==3
replace employment_status = "full-time" if Q94==4 | Q94==5
drop Q94 

*** Assets *** 
gen owns_radio = 0 if Q93B!=. & Q93B!=-1 & Q93B!=9
replace owns_radio = 1 if Q93B==1

gen owns_tv = 0 if Q93C!=. & Q93C!=-1 & Q93C!=9
replace owns_tv = 1  if Q93C==1

gen owns_vehicle = 0 if Q93F!=. & Q93F!=-1 & Q93F!=9
replace owns_vehicle = 1  if Q93F==1
drop Q93A-Q93F


**************************
***   Interest index   ***
**************************


gen discuss_pol_index = Q17 if Q17 >= 0 & Q17 <= 2 

gen interest_pub_affairs_index = 0 if Q16>= 0 & Q16 <= 3
replace interest_pub_affairs_index = 1 if Q16==2 | Q16==3
replace interest_pub_affairs_index = 2 if Q16==3


**** Information and Media ***

gen radio = Q15A if Q15A!=-1 & Q15A!=. & Q15A!=9 & Q15A != 998
gen tv = Q15A if Q15B!=-1 & Q15B!=. & Q15B!=9 & Q15B != 998
gen newspaper = Q15C if Q15C!=-1 & Q15C!=. & Q15C!=9 & Q15C != 998

gen radio_news_often =0 if Q15A!=-1 & Q15A!=. & Q15A!=9 & Q15A != 998
replace  radio_news_often =1 if Q15A==4

gen tv_news_often =0 if Q15B!=-1 & Q15B!=. & Q15B!=9 & Q15B != 998
replace  tv_news_often =1 if Q15B==4

gen newspaper_news_often =0 if Q15C !=-1 & Q15C !=. & Q15C !=9 & Q15C != 998
replace  newspaper_news_often =1 if Q15C ==4


*************************
***   Participation   ***
*************************

gen contact_LG_councilor_index = Q32A if Q32A!=. & Q32A!=-1 & Q32A!=9 & Q32A != 998

gen contact_MP_index = Q32B if Q32B!=. & Q32B!=-1 & Q32B!=9 & Q32B != 998

gen contact_officials_index = Q32C if Q32C!=. & Q32C!=-1 & Q32C!=9 & Q32C != 998

gen att_demonstration_index = Q31C if Q31C!=. & Q31C!=-1 & Q31C!=9 & Q31C != 998

gen raise_issue_index = Q31B  if Q31B!=. & Q31B!=-1 & Q31B!=9 & Q31B != 998

gen vote1 = 0 if Q30 !=-1 & Q30 !=9 &  Q30 !=.
replace vote1 = 1 if Q30==1

gen close_pds = 0 if Q86 <= 997 & Q86 != . & Q86 != -1
replace close_pds = 1 if Q86 == 360


***********************************
***   Democratic institutions   ***
***********************************

gen trust_EC=Q55C if Q55C!=. & Q55C!=-1 &  Q55C!=9

gen trust_courts=Q55I if Q55I !=. & Q55I !=-1 & Q55I!=9

gen careful_say = Q53A if Q53A !=. & Q53A!= -1 & Q53A!=9 

gen elections_free_fair = Q45 if Q45 < 8 & Q45 >= 0
recode elections_free_fair (4=1) (3=2) (2=3) (1=4)

gen how_democratic=0 if Q46!=.  & Q46!=-1  & Q46!=9
replace how_democratic=1 if Q46==3
replace how_democratic=2 if Q46==4
drop Q46 

gen satisfaction_democracy = 0 if Q47!=. & Q47!=-1 & Q47!=9
replace satisfaction_democracy = 1 if Q47==3
replace satisfaction_democracy = 2 if Q47==4

gen checks_balances =0 if Q40!=. &  Q40!=-1 & Q40!=9
replace checks_balances =1 if Q40==1 |  Q40==2

gen checks_balances_strong =0 if  Q40!=. &  Q40!=-1 & Q40!=9
replace checks_balances_strong =1 if Q40==1
drop Q40


gen against_govt_ban_org=0 if Q25!=-1 & Q25!=. & Q25!=9
replace against_govt_ban_org=1 if Q25==3| Q25==4

gen against_govt_ban_org_str=0 if Q25!=-1 & Q25!=. &Q25!=9
replace against_govt_ban_org_str=1 if Q25==4
drop Q25

gen against_president_discretion =0 if Q41!=. & Q41!=-1 &  Q41!=9
replace against_president_discretion =1 if Q41==3 |  Q41==4

gen against_pres_discr_strong =0 if Q41!=. & Q41!=-1 & Q41!=9
replace against_pres_discr_strong =1 if Q41==4
drop Q41


gen against_one_party_rule =0 if Q36A!=. & Q36A!=-1 & Q36A!=9
replace against_one_party_rule =1 if Q36A == 1 | Q36A==2

gen a_one_party_rule_strong =0 if Q36A!=. & Q36A!=-1 & Q36A!=9
replace a_one_party_rule_strong =1 if Q36A == 1 

gen reject_military_rule =0 if Q36B!=. & Q36B!=-1 & Q36B!=9
replace reject_military_rule =1 if Q36B == 1 | Q36B==2

gen r_mil_rule_str =0 if Q36B!=. & Q36B!=-1 & Q36B!=9
replace r_mil_rule_str =1 if Q36B == 1

gen reject_one_man_rule =0 if Q36C!=. & Q36C!=-1 &  Q36C!=9
replace reject_one_man_rule =1 if Q36C == 1 | Q36C==2

gen r_one_man_r_strongly =0 if Q36C!=. & Q36C!=-1 & Q36C!=9
replace r_one_man_r_strongly =1 if Q36C == 1 
drop Q36*


gen election_choice_leaders = 0 if Q38!=.  & Q38!=-1  & Q38!=9
replace election_choice_leaders = 1 if Q38==1  | Q38==2

gen election_choice_leaders_str = 0 if Q38!=.  & Q38!=-1  & Q38!=9
replace election_choice_leaders_str = 1 if Q38==1 
drop Q38

gen newspaper_free=0 if Q26!=-1 & Q26!=. &Q26!=9
replace newspaper_free=1 if Q26==1 | Q26 ==2 

gen newspaper_free_str=0 if Q26!=-1 & Q26!=. &Q26!=9
replace newspaper_free_str=1 if Q26 ==1
drop Q26


*** Trust in different institutions ***

gen trust_president =Q55A if Q55A!=. & Q55A!=-1 & Q55A!=9

gen trust_national_assembly=Q55B if Q55B!=. &  Q55B!=-1 & Q55B!=9

gen trust_LG_council=Q55D if Q55D!=. & Q55D!=-1 & Q55D!=9

gen trust_ruling_party=Q55E if Q55E!=. & Q55E!=-1 & Q55E!=9

gen trust_opposition_party=Q55F if Q55F!=. & Q55F!=-1 & Q55F!=9

drop Q55*

*** Government performance ***

gen present_econ = Q4A - 1 if Q4A!=. & Q4A!=-1 & Q4A!=9
label var present_econ "Economic conditions"

gen personal_econ = Q4B - 1 if Q4B!=. & Q4B!=-1 & Q4B!=9
label var personal_econ "Personal economic conditions"

gen past_econ = Q6A - 1 if Q6A!=. & Q6A!=-1 & Q6A!=9
label var past_econ "Compare econ, past 12mths"

gen future_econ = Q7A - 1 if Q7A!=. & Q7A!=-1 & Q7A!=9
label var future_econ "Future econ (12mth)"

gen handles_economy =Q65A-1 if Q65A!=. & Q65A!=-1 & Q65A!=9
label var handles_economy "Government handles economy"

gen handles_employment=Q65B-1  if Q65B!=. & Q65B!=-1 & Q65B!=9
label var handles_employment "Government handles employment"

gen handles_inflation=Q65C-1 if Q65C!=. & Q65C!=-1 & Q65C!=9
label var handles_inflation "Government handles inflation"

gen handles_inequality=Q65D-1 if Q65D!=. & Q65D!=-1 & Q65D!=9
label var handles_inequality "Government handles inequality"

gen handles_health=Q65F-1 if Q65F!=. & Q65F!=-1 & Q65F!=9
label var handles_health "Government handles health provision"

gen handles_education=Q65G-1 if Q65G!=. & Q65G!=-1 & Q65G!=9
label var handles_education "Government handles education provision"

gen handles_water=Q65H-1 if Q65H!=. & Q65H!=-1 & Q65H!=9
label var handles_water "Government handles water provision"
drop Q65*

gen loc_gov_handles_roads=Q67A-1  if Q67A!=. & Q67A!=-1 & Q67A!=9
label var loc_gov_handles_roads "Local government handles roads"

gen loc_gov_handles_clean=Q67B-1 if Q67B!=. & Q67B!=-1 & Q67B!=9
label var loc_gov_handles_clean "Local government handles community cleaning"

* Up to 2005 the variable was split in fees and poperty tax, but in 2005 it was aggregated as local taxes
gen loc_gov_handles_local_tax=Q67C-1  if Q67C!=. & Q67C!=-1 & Q67C!=9
label var loc_gov_handles_local_tax "Local government handles collecting local taxes"
drop Q67*

gen performance_president = Q68A-1 if Q68A!=. & Q68A!=-1 & Q68A!=9
label var performance_president "Approves President perfomance"
 
gen performance_mp  = Q68B-1 if Q68B!=. & Q68B!=-1 & Q68B!=9
label var performance_mp "Approves MP perfomance"

gen performance_LG_councilor  = Q68C-1 if Q68C!=. & Q68C!=-1 & Q68C!=9
label var performance_LG_councilor "Approves LG councilor perfomance"
drop Q68*

gen MP_listen = Q62A if Q62A !=. & Q62A !=-1 & Q62A !=9
label var  MP_listen "MPs listen to people's interests"

gen LG_councilor_listen = Q62B if Q62B !=. & Q62B !=-1 & Q62B !=9
label var  LG_councilor_listen "LG Councilors listen to people's interests"
drop Q62*


gen corruption_president = Q56A if Q56A !=. &  Q56A !=-1 &  Q56A !=9
label var corruption_president "Very corrupt president office"

gen corruption_MP = Q56B if Q56B !=. &  Q56B !=-1 &  Q56B !=9
label var corruption_MP "Very corrupt MPs"

gen corruption_LG_councilors = Q56C if Q56C !=. &  Q56C !=-1 &  Q56C !=9 
label var corruption_LG_councilors "Very corrupt government officials"

gen corruption_government_officials = Q56D if Q56D  !=. &  Q56D  !=-1 &  Q56D  !=9
label var corruption_government_officials "Very corrupt LG councilors"

gen corr_pres_dk = Q56A == 9  if Q56A !=. &  Q56A !=-1
gen corr_mp_dk = Q56B == 9  if Q56B !=. &  Q56B !=-1
gen corr_lg_dk = Q56C == 9  if Q56C !=. &  Q56C !=-1
gen corr_gov_dk = Q56D == 9  if Q56D !=. &  Q56D !=-1


drop Q2-Q118
drop region-eanumb

